package com.futureCloudMOM.common.utils.core;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toCollection;

/**
 * 条件构造类  用于高级检索功能的条件拼接
 */
@Slf4j
public class QueryWapperUtils {


    public static <T> QueryWrapper<T> wapperJojnt(Object object, List<QueryWapperDTO> list) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(list) & list.size() > 0) {
            list = list.stream().collect(Collectors.collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(QueryWapperDTO::getPropertyName))), ArrayList::new));
            List<QueryWapperDTO> queryWapperDTOList = new ArrayList<>();
            //for (QueryWapperDTO q : list) {
            //    for (QueryWapperDTO j : list) {
            //        if (q.getPropertyName().equals(j.getPropertyName()) & (!q.getJojntType().equals(j.getJojntType()) | !q.getPrice().equals(j.getPrice()))) {
            //            queryWapperDTOList.add(q);
            //        }
            //    }
            //}
            queryWrapper = joint(object,list,queryWapperDTOList);
        }
        return queryWrapper;
    }

    /**
     *
     * @param object 拼接对象
     * @param list 前端传上来的数据进行去重后得到的数据
     * @param queryWapperDTOS  去重抽出来的数据
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> joint(Object object,List<QueryWapperDTO> list,List<QueryWapperDTO> queryWapperDTOS) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
            for (QueryWapperDTO queryWapperDTO : list) {
                switch (queryWapperDTO.getJojntType()) {
                    case "eq":
                        queryWrapper.eq(queryWapperDTO.getPropertyName(), queryWapperDTO.getPrice());
                        break;
                    case "ne":
                        queryWrapper.ne(queryWapperDTO.getPropertyName(), queryWapperDTO.getPrice());
                        break;
                    case "like":
                        queryWrapper.like(queryWapperDTO.getPropertyName(), queryWapperDTO.getPrice());
                        break;
                    case "ge":
                        try {
                            queryWrapper = wapperJojntByGeLe(object, queryWapperDTO, "ge");
                        } catch (Exception e) {
                            throw new RuntimeException(e.getMessage());
                        }
                        break;
                    case "le":
                        try {
                            queryWrapper = wapperJojntByGeLe(object, queryWapperDTO, "le");
                        } catch (Exception e) {
                            throw new RuntimeException(e.getMessage());
                        }
                        break;
                }
            }
        return queryWrapper;
    }

    /**
     * 大于等于只可用于对时间和数字类型做数据筛选
     *
     * @return
     */
    public static <T> QueryWrapper<T> wapperJojntByGeLe(Object object, QueryWapperDTO queryWapperDTO, String code) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(queryWapperDTO.getPropertyName())) {
                Class<?> c = field.getType();
                if (c.getName().equals("java.lang.String")) {
                    if (code.equals("ge")) {
                        throw new RuntimeException("大于等于仅支持数字和时间类型检索");
                    } else {
                        throw new RuntimeException("小于等于仅支持数字和时间类型检索");
                    }
                } else if (c.getName().equals("java.long.Long")) {
                    if (code.equals("ge")) {
                        queryWrapper.ge(queryWapperDTO.getPropertyName(), Long.parseLong(queryWapperDTO.getPrice()));
                    } else {
                        queryWrapper.le(queryWapperDTO.getPropertyName(), Long.parseLong(queryWapperDTO.getPrice()));
                    }
                    break;
                } else if (c.getName().equals("java.math.BigDecimal")) {
                    if (code.equals("ge")) {
                        queryWrapper.ge(queryWapperDTO.getPropertyName(), new BigDecimal(queryWapperDTO.getPrice()));
                    } else {
                        queryWrapper.le(queryWapperDTO.getPropertyName(), new BigDecimal(queryWapperDTO.getPrice()));
                    }
                    break;
                }
            }
        }
        if (code.equals("ge")) {
            queryWrapper.ge(queryWapperDTO.getPropertyName(), queryWapperDTO.getPrice());
        } else {
            queryWrapper.le(queryWapperDTO.getPropertyName(), queryWapperDTO.getPrice());
        }
        return queryWrapper;
    }
}
